package com.bbs.toutiao.service.admin.impl;



import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import com.bbs.toutiao.aspect.SysLog;
import com.bbs.toutiao.entity.UserEntity;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.mapper.admin.UserMapper;
import com.bbs.toutiao.utils.Result;
import com.bbs.toutiao.vo.user.RegisterVo;
import com.bbs.toutiao.vo.user.LoginRequestVo;

import com.bbs.toutiao.service.admin.UserService;
import com.bbs.toutiao.vo.user.UpdateStatusVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * 描述:
 * 作者： zhangBin
 * 时间： 2023/9/6 10:04
 * 版本： 1.0
 */

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    /** 登陆
     * @param loginVo 登陆响应类
     * @return {@link UserEntity}
     * 作者: zhang
     * Date: 2023/9/17 10:32
     */
    @SysLog("管理员登录后台管理系统")
    @Override
    public UserEntity login(LoginRequestVo loginVo) {
        String password = MD5.create().digestHex(loginVo.getPassword(), StandardCharsets.UTF_8);
        String email = loginVo.getEmail();
        UpdateStatusVo statusVo=new UpdateStatusVo();
        long cur = DateUtil.currentSeconds();  //获取当前时间戳
        //获取用户信息
        UserEntity user = userMapper.login(email,password);
        if (user == null) {
            throw new BaseException(ExceptionEnum.NOT_FOUND);
        }
        if (user.getPassword().equals(password)&& user.getUserEmail().equals(loginVo.getEmail())) {
            int sta = user.getStatus();
            userStatus(statusVo, cur, user, sta, userMapper, log);
            //判断登录权限
            if (Objects.equals(user.getRole(), "ADMIN")){
                return user;
            }else {
                throw new BaseException(ExceptionEnum.LARGE_NO_TO);
            }

        } else {
            log.error("用户信息不匹配，不具备登录条件");
            throw new BaseException(ExceptionEnum.USER_NOT_PASSWORD);
        }

    }

    //解析用户是是否被限制登录
    public static void userStatus(UpdateStatusVo statusVo, long cur, UserEntity user, int sta, UserMapper userMapper, Logger log) {
        switch (sta){
            case 1:
                DateTime time = DateUtil.parse(user.getBanEndTime());
                long exp = time.getTime()/1000; //到期时间戳
//                    log.info("时间格式为："+cur+","+exp+","+user.getBanEndTime());
                if (cur - exp >= 0) {
                    statusVo.setId(user.getId());
                    statusVo.setStatus(0);
                    statusVo.setAccount(user.getAccount());
                    //封禁已经到期
                    userMapper.updateStatus(statusVo);
                    log.info("用户状态已解封");
                    break;
                } else {
                    throw new BaseException(ExceptionEnum.USER_OFF_A);
                }
            case 2:
                throw new BaseException(ExceptionEnum.USER_OFF_B);
            default:
                break;
        }
    }


    /**
     * 用户注册
     *     Transactional 抛出异常将会回滚事务
     * @param registerVo 用户注册实体类
     * @return Boolean
     */
    @Transactional
    @Override
    public Boolean registerUser(RegisterVo registerVo) {
       return register(registerVo,userMapper);
    }

    public static boolean register(RegisterVo registerVo,UserMapper userMapper){
        String password = MD5.create().digestHex(registerVo.getPassword(), StandardCharsets.UTF_8);
        String account = String.valueOf(DateUtil.currentSeconds()); //生成用户账号
        registerVo.setPassword(password);
        registerVo.setAccount(account);
        int i = userMapper.register(registerVo);
        return i == 1;
    }
}
